<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="region"/>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.js"></script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">旅游区域管理</h1>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-12">
                    <a role="button" class="btn btn-success inputBtn">
                        <span class="glyphicon glyphicon-plus"></span> 添加区域
                    </a>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-lg-8">
                    <table class="table table-striped table-hover" style="display: none">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>区域</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>xx</td>
                                <td>xx</td>
                                <td>
                                    <a href="javascript:"  class="btn btn-info btn-xs inputBtn">
                                        <span class="glyphicon glyphicon-pencil"></span> 修改
                                    </a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal" method="post" action="/region/saveOrUpdate.do">
                    <input type="hidden" name="id" value="" />
                    <div class="form-group">
                        <label class="col-sm-4 control-label">区域名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入区域名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级区域</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parent.name" readonly/>
                            <input type="hidden" class="form-control" name="parent.id" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设为推荐</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:" class="btn btn-success submitBtn" aria-hidden="true">保存</a>
                <a href="javascript:" class="btn btn-info" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        //克隆右侧的tr
        var tr = $("table tbody tr:first").clone();

        $.get("/region/queryTreeview.do", {treeview: true}, function (data) {
            $("#treeview").treeview({
                color: "#428bca",
                showTags: true, //显示徽章
                lazyLoad: function (node) { //参数1:就是当前点击的节点
                    //发送ajax请求,获取当前节点的子节点
                    $.get("/region/queryTreeview.do", {parentId: node.id, treeview: true}, function (data) {
                        //调用treeview的addNode方法来完成添加子节点
                        /* treeview函数
                            参数1:调用treeview中哪个方法
                            参数2:调用该方法使用的参数,该参数是一个数组

                                  索引0:需要添加哪些数据
                                  索引1:添加到哪个节点中
                         */
                        $("#treeview").treeview("addNode", [data, node]);
                    });
                },
                onNodeSelected: function (ev, node) { //当点击节点触发点击事件
                    $.get("/region/queryTreeview.do", {parentId: node.id || null}, function (data) {
                        //显示右侧的数据
                        $("table").show();
                        //先清空数据
                        $("table tbody").html("");
                        //再填充新的数据
                        $.each(data, function (i, ele) {
                       /* <tr>
                            <td>xx</td>
                            <td>xx</td>
                            <td>
                                <a href="javascript:" class="btn btn-info btn-xs inputBtn">
                                        <span class="glyphicon glyphicon-pencil"></span> 修改
                                </a>
                            </td>
                        </tr>*/
                            var row = tr.clone(); //再次克隆
                            row.children("td")[0].innerHTML = i + 1;
                            row.children("td")[1].innerHTML = ele.name;
                            row.find("a").attr("data-json", ele.json);
                            row.appendTo("table tbody");
                        });
                    });
                },
                data: [{
                    text: "全部区域",
                    nodes: data
                }]
            });
        });
        
        //点击添加区域弹出模态框
        $(".inputBtn").click(function () {
            show();
        });
        
        //点击保存按钮保存数据
        $(".submitBtn").click(function () {
            $("#editForm").submit();
        });

        //动态事件绑定
        $("tbody").on("click", ".inputBtn", function () {
            var json = $(this).data("json");
            show(json); //用于数据回显
        });
        
        function show(json) {
            //清空缓存数据
            $("#editForm input").val("");

            $("#inputModal").modal("show");
            var node = $('#treeview').treeview('getSelected')[0];
            if (!node) { //没有选择节点的特殊处理
                $("#editForm input[name='parent.name']").val("全部区域");
                return;
            }
            //有选择某个父节点
            $("#editForm input[name='parent.name']").val(node.text);
            $("#editForm input[name='parent.id']").val(node.id);
            if (json) { //有JSON说明当前的修改
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm select[name='state']").val(json.state);
            }
        }
    });
</script>
</body>
</html>